/*
 *   Copyright (C) 2006-2007 Jiri Majer. All Rights Reserved.
 *   DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *   This code is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License version 2 only, as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.diabetesdiary.calendar.ui;

import java.text.NumberFormat;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.diabetesdiary.diary.domain.Insulin;
import org.diabetesdiary.diary.utils.MyLookup;
import org.diabetesdiary.diary.service.db.InsulinDO;
import org.openide.util.NbBundle;

public final class NewPatientVisualPanel2 extends JPanel {
    
    private NumberFormat doubleFormat;
    
    class MyDocumentListener implements DocumentListener {
        
        @Override
        public void insertUpdate(DocumentEvent e) {
            NewPatientVisualPanel2.this.wizard.fireChangeEvent();
        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            NewPatientVisualPanel2.this.wizard.fireChangeEvent();
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            //Plain text components don't fire these events
        }
    }
    
    private NewPatientWizardPanel2 wizard;
    
    /** Creates new form NewPatientVisualPanel2 */
    public NewPatientVisualPanel2(NewPatientWizardPanel2 wizard) {
        this.wizard = wizard;
        doubleFormat = NumberFormat.getInstance();
        doubleFormat.setMaximumFractionDigits(1);
        initComponents();
        hepSensitivity.setValue(0.5d);
        perSensitivity.setValue(0.5d);
        threshold.setValue(9.0d);
        rate.setValue(100.0d);
        hepSensitivity.getDocument().addDocumentListener(new MyDocumentListener());
        perSensitivity.getDocument().addDocumentListener(new MyDocumentListener());
        threshold.getDocument().addDocumentListener(new MyDocumentListener());
        rate.getDocument().addDocumentListener(new MyDocumentListener());
    }
    
    @Override
    public String getName() {
        return NbBundle.getMessage(NewPatientVisualPanel2.class,"newpatient2title");
    }
    
    private ComboBoxModel createInsulinCombo(){
        ComboBoxModel model = new DefaultComboBoxModel(MyLookup.getDiaryRepo().getInsulines().toArray());
        return model;
    }
    
    public Double getPerSensitivity() {
        return ((Number)perSensitivity.getValue()).doubleValue();
    }
    
    public void setPerSensitivity(Double perSensitivity) {
        this.perSensitivity.setValue(perSensitivity);
    }
    
    public Double getHepSensitivity() {
        return ((Number)hepSensitivity.getValue()).doubleValue();
    }
    
    public void setHepSensitivity(Double hepSensitivit) {
        hepSensitivity.setValue(hepSensitivit);
    }
    
    public Double getFiltrationRate() {
        return ((Number)rate.getValue()).doubleValue();
    }
    
    public void setFiltrationRate(Double filtrationRate) {
        rate.setValue(filtrationRate);
    }
    
    public Double getRenalThreshold() {
        return ((Number)threshold.getValue()).doubleValue();
    }
    
    public void setRenalThreshold(Double renalThreshold) {
        threshold.setValue(renalThreshold);
    }
    
    public Insulin getBasalInsulin(){
        return (Insulin) basal.getSelectedItem();
    }
    
    public Insulin getBolusInsulin(){
        return (Insulin) bolus.getSelectedItem();
    }
    
    public void setBasalInsulin(Insulin ins){
        basal.setSelectedItem(ins);
    }
    
    public void setBolusInsulin(Insulin ins){
        bolus.setSelectedItem(ins);
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        bolus = new javax.swing.JComboBox();
        basal = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        threshold = new JFormattedTextField(doubleFormat);
        hepSensitivity = new JFormattedTextField(doubleFormat);
        perSensitivity = new JFormattedTextField(doubleFormat);
        rate = new JFormattedTextField(doubleFormat);
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(NbBundle.getMessage(NewPatientVisualPanel2.class,"typeofinsulin"))));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, NbBundle.getMessage(NewPatientVisualPanel2.class,"rapidinsulin"));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel8, NbBundle.getMessage(NewPatientVisualPanel2.class,"lenteinsulin"));

        bolus.setModel(createInsulinCombo());
        bolus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bolusActionPerformed(evt);
            }
        });

        basal.setModel(createInsulinCombo());
        basal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                basalActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel7)
                    .add(jLabel8))
                .add(29, 29, 29)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(basal, 0, 170, Short.MAX_VALUE)
                    .add(bolus, 0, 170, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel7)
                    .add(bolus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(basal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(NbBundle.getMessage(NewPatientVisualPanel2.class,"simulatorparameter")));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, NbBundle.getMessage(NewPatientVisualPanel2.class,"renaltreshold"));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, NbBundle.getMessage(NewPatientVisualPanel2.class,"renalfunction"));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, NbBundle.getMessage(NewPatientVisualPanel2.class,"peripherysensitivity"));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, NbBundle.getMessage(NewPatientVisualPanel2.class,"hepaticsensitivity"));

        threshold.setColumns(5);
        threshold.setText("9.0");

        hepSensitivity.setColumns(5);
        hepSensitivity.setText("0.5");

        perSensitivity.setColumns(5);
        perSensitivity.setText("0.5");

        rate.setColumns(5);
        rate.setText("100");

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, NbBundle.getMessage(NewPatientVisualPanel2.class,"renalvalues"));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, NbBundle.getMessage(NewPatientVisualPanel2.class,"tip"));

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel4)
                                    .add(jLabel3)
                                    .add(jLabel1)
                                    .add(jLabel2))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(threshold, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(hepSensitivity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(perSensitivity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(rate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(jLabel5)))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(34, 34, 34)
                        .add(jLabel6)))
                .addContainerGap(82, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jLabel6)
                .add(17, 17, 17)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(perSensitivity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(hepSensitivity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(threshold, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(rate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void bolusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bolusActionPerformed
        NewPatientVisualPanel2.this.wizard.fireChangeEvent();
    }//GEN-LAST:event_bolusActionPerformed
    
    private void basalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_basalActionPerformed
        NewPatientVisualPanel2.this.wizard.fireChangeEvent();
    }//GEN-LAST:event_basalActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox basal;
    private javax.swing.JComboBox bolus;
    private javax.swing.JFormattedTextField hepSensitivity;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JFormattedTextField perSensitivity;
    private javax.swing.JFormattedTextField rate;
    private javax.swing.JFormattedTextField threshold;
    // End of variables declaration//GEN-END:variables
    
}

